package ch.nostromo.tiffanys.lib;

import ch.nostromo.lib.util.NosTools;
import ch.nostromo.tiffanys.game.board.Board;
import ch.nostromo.tiffanys.game.board.BoardTools;

public class ChessTools {

  public static String dumpBoard(Board boardo) {
    int[] board = boardo.getBoardArray();
    String result = "";
    result += "\n";
    result += dumpRow(board, 91, 98) + "\n";
    result += dumpRow(board, 81, 88) + "\n";
    result += dumpRow(board, 71, 78) + "\n";
    result += dumpRow(board, 61, 68) + "\n";
    result += dumpRow(board, 51, 58) + "\n";
    result += dumpRow(board, 41, 48) + "\n";
    result += dumpRow(board, 31, 38) + "\n";
    result += dumpRow(board, 21, 28) + "\n";
    result += "  A  B  C  D  E  F  G  H \n";
    return result;

  }

  public static String dumpBoard(int[] board) {
    String result = "";
    result += "\n";
    result += dumpRow(board, 91, 98) + "\n";
    result += dumpRow(board, 81, 88) + "\n";
    result += dumpRow(board, 71, 78) + "\n";
    result += dumpRow(board, 61, 68) + "\n";
    result += dumpRow(board, 51, 58) + "\n";
    result += dumpRow(board, 41, 48) + "\n";
    result += dumpRow(board, 31, 38) + "\n";
    result += dumpRow(board, 21, 28) + "\n";
    result += "  A  B  C  D  E  F  G  H \n";
    return result;

  }

  private static String getFieldValueDescription(int field) {
    if (NosTools.checkMask(field, Board.EMPTY_FIELD))
      return "[] ";

    String result = "";
    if (NosTools.checkMask(field, Board.OWNER_WHITE)) {
      result += "W";
    } else {
      result += "B";
    }

    result += getPieceDescription(field);
    result += " ";

    return result;
  }


  public static String getPieceDescription(int field) {
    String result = "";
    if (NosTools.checkMask(field, Board.PIECE_KING))
      result = "K";
    if (NosTools.checkMask(field, Board.PIECE_QUEEN))
      result = "Q";
    if (NosTools.checkMask(field, Board.PIECE_BISHOP))
      result = "B";
    if (NosTools.checkMask(field, Board.PIECE_KNIGHT))
      result = "N";
    if (NosTools.checkMask(field, Board.PIECE_ROOK))
      result = "R";
    if (NosTools.checkMask(field, Board.PIECE_PAWN))
      result = "P";
    return result;
  }

  public static String getPieceDescriptionByCode(int pieceCode) {
    String result = "";
    if (pieceCode == Board.PIECE_KING)
      result = "k";
    if (pieceCode == Board.PIECE_QUEEN)
      result = "q";
    if (pieceCode == Board.PIECE_BISHOP)
      result = "b";
    if (pieceCode == Board.PIECE_KNIGHT)
      result = "n";
    if (pieceCode == Board.PIECE_ROOK)
      result = "r";
    if (pieceCode == Board.PIECE_PAWN)
      result = "p";
    return result;
  }

  public static int getPieceCode(int field) {
    int result = 0;
    if (NosTools.checkMask(field, Board.PIECE_KING))
      result = Board.PIECE_KING;
    if (NosTools.checkMask(field, Board.PIECE_QUEEN))
      result = Board.PIECE_QUEEN;
    if (NosTools.checkMask(field, Board.PIECE_BISHOP))
      result = Board.PIECE_BISHOP;
    if (NosTools.checkMask(field, Board.PIECE_KNIGHT))
      result = Board.PIECE_KNIGHT;
    if (NosTools.checkMask(field, Board.PIECE_ROOK))
      result = Board.PIECE_ROOK;
    if (NosTools.checkMask(field, Board.PIECE_PAWN))
      result = Board.PIECE_PAWN;
    return result;
  }

  private static String dumpRow(int[] board, int beg, int end) {
    String result = BoardTools.getCollumnDescription(beg) + " ";
    for (int i = beg; i <= end; i++) {
      result += getFieldValueDescription(board[i]);
    }
    return result;
  }

}